package com.wing.cabinet.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.StorageInventoryKnife;
import com.wing.cabinet.model.response.StorageInventoryKnifeVO;
import com.wing.cabinet.service.StorageInventoryKnifeService;
import com.wing.cabinet.utils.ExcelUtils;
import com.wing.common.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-刀具产品货道库存管理", tags = "#/cabinet/storageInventoryKnifeRepair")
@RestController("storageInventoryKnifeRepairController")
@RequestMapping("cabinet/admin/storageInventoryKnifeRepair")
@RequiredArgsConstructor
public class StorageInventoryKnifeRepairController {

    private final StorageInventoryKnifeService storageInventoryKnifeService;


    @ApiOperation(value = "查询所有维修刀具产品货道库存", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<StorageInventoryKnifeVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String skuCode
    ){
        Page<StorageInventoryKnife> pg = new Page<>(page, limit);
        Map queryParam=new HashMap();
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("skuCode",skuCode);
        queryParam.put("businessType",StorageInventoryKnife.BusinessType.repair.getValue());
        IPage<StorageInventoryKnifeVO> list = storageInventoryKnifeService.findStorageInventoryKnifeList(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        List<StorageInventoryKnifeVO> storageInventoryKnifeVOList = new ArrayList<>();
        Map paramsMap = new HashMap();
        paramsMap.put("businessType",StorageInventoryKnife.BusinessType.repair.getValue());
        storageInventoryKnifeVOList = storageInventoryKnifeService.ecxportList(paramsMap);
        //导出
        ExcelUtils.export(response,"报修在柜物料",storageInventoryKnifeVOList, StorageInventoryKnifeVO.class);
    }

}
